package xhwl.insurance;

import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.trello.rxlifecycle.components.RxActivity;
import com.umeng.analytics.MobclickAgent;

import xhwl.insurance.util.DimenUtil;
import xhwl.insurance.util.ImmerseUtil;


/**
 * Created by admin on 2016/12/20.
 */

public abstract class BaseActivity extends RxActivity {
    protected Context mContext;
    protected RelativeLayout titleLayout;
    protected TextView titleTextView;
    protected ImageView backImageView;
    protected ImageView rightImageView;
    protected TextView rightTextView;
    protected TextView leftTextView;
    protected LinearLayout llBack;


    protected ImmerseUtil immerseUtil;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        mContext = this;
        MyApp.getInstance().addActivity(this);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 
//        DimenUtil.setStatusBar(this);
        MobclickAgent.setDebugMode(true);
        MobclickAgent.openActivityDurationTrack(false);
        MobclickAgent.setScenarioType(mContext, MobclickAgent.EScenarioType.E_UM_NORMAL);

    }
    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);       //统计时长
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
    @Override
    public void setContentView(int layoutResID) {
        // TODO Auto-generated method stub
        super.setContentView(layoutResID);
        initViews();
        initListener();
        processOper();
        loadData();
    }

    protected void initTitleViews() {
        titleLayout = (RelativeLayout) findViewById(R.id.title_layout);
        titleTextView = (TextView) findViewById(R.id.title_textview);
        backImageView = (ImageView) findViewById(R.id.title_left_imageview);
        rightImageView = (ImageView) findViewById(R.id.title_right_imageview);
        rightTextView = (TextView) findViewById(R.id.title_right_textview);
        leftTextView=(TextView) findViewById(R.id.title_left_textview);
        llBack = (LinearLayout) findViewById(R.id.llBack);
        llBack.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                onBackPressed();
            }
        });
        setUpTitleView();
        if (isImmerseLayout()) {

            if(titleLayout!=null){
                immerseUtil=new ImmerseUtil(this);
                immerseUtil.setImmerseLayout(titleLayout);
            }
        }

    }

    protected boolean isImmerseLayout() {
        return true;
    }
    protected boolean handleETTouchEvent() {
        return true;
    }

    public abstract void initViews();

    protected void setUpTitleView() {
    }

    public abstract void initListener();

    protected void processOper() {
    }

    public abstract void loadData();

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)&&handleETTouchEvent()) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

}
